package TCPSocket;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author 王艺锡
 * @version 1.0
 */
public class SocketTCP04Server {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8888);
        System.out.println("服务端，在8888端口监听，等待连接...");
        String filePath = "src\\image.png";
        FileOutputStream fileOutputStream = new FileOutputStream(filePath);

        Socket socket = serverSocket.accept();
        InputStream inputStream = socket.getInputStream();
        byte buf[] = new byte[1024];
        int readLen = 0;

        while ((readLen = inputStream.read(buf)) != -1){
                fileOutputStream.write(buf,0,readLen);
        }

        OutputStream outputStream = socket.getOutputStream();
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(outputStream));
        bw.write("收到图片..");
        bw.flush();
        socket.shutdownOutput();

        //5.关闭对应的流和socket
        bw.close();
        fileOutputStream.close();
        inputStream.close();
        socket.close();
        serverSocket.close();

    }
}
